//
//  SqStackExt.cpp
//  DataStructure
//
//  Created by 舒磊明 on 2020/6/26.
//  Copyright © 2020 Shuleiming. All rights reserved.
//

#include "SqStackExt.hpp"

// I代表入栈，O代表出栈，判断出，入栈否合法
bool JudgePushPop(char *str)
{
    int i = 0;
    int k = 0;
    while (str[i] != '\0') {
        if (str[i] == 'I') k++;
        else k--;
        if (k < 0) return false;
        i++;
    }
    return k == 0;
}

// 判断是否对称
bool dc(LinkList L, int n)
{
    // L为单链表，n为长度
    char c[n / 2];      // 栈
    LNode *p = L->next; // 工作指针
    int i = 0;
    for (; i < n / 2; i++) {
        c[i] = p->data;
        p = p->next;
    }
    if (n % 2 == 1) p = p->next; // 若n为奇数则向后移动
    i--; // 指向栈顶
    while (p != nullptr && p->data == c[i]) {
        p = p->next;
        i--;
    }
    return i == -1;
}
